<template>
  <div class="carousel-container">
    <div class="carousel-wrapper" @mouseenter="pauseCarousel" @mouseleave="startCarousel">
      <div 
        v-for="(item, index) in items" 
        :key="item.id" 
        class="carousel-item" 
        :class="{ active: index === currentIndex }"
        @click="handleCarouselClick(item.link)"
      >
        <!-- 模糊背景作为加载效果 -->
        <div class="carousel-bg" :style="{ backgroundImage: `url(${item.imageBlur})` }"></div>
        <!-- 主图片 -->
        <img v-img-error :src="item.image" :alt="item.title" class="carousel-image">
        <div class="carousel-caption">
          <!-- 徽章 -->
          <span v-if="item.badge" class="carousel-badge">{{ item.badge }}</span>
          <h3>{{ item.title }}</h3>
          <p>{{ item.description }}</p>
        </div>
      </div>
    </div>
    <div class="carousel-indicators">
      <button 
        v-for="(item, index) in items" 
        :key="item.id" 
        class="indicator" 
        :class="{ active: index === currentIndex }"
        @click="goToSlide(index)"
      ></button>
    </div>
    <!-- 左右箭头 -->
    <button class="carousel-arrow prev" @click="prevSlide">
      &lt;
    </button>
    <button class="carousel-arrow next" @click="nextSlide">
      &gt;
    </button>
  </div>
</template>

<script>
export default {
  name: 'Carousel',
  props: {
    items: {
      type: Array,
      required: true,
    },
  },
  data() {
    return {
      currentIndex: 0,
      carouselInterval: null,
    }
  },
  mounted() {
    this.startCarousel()
  },
  beforeUnmount() {
    this.pauseCarousel()
  },
  methods: {
    startCarousel() {
      this.carouselInterval = setInterval(() => {
        this.nextSlide()
      }, 5000)
    },
    pauseCarousel() {
      if (this.carouselInterval) {
        clearInterval(this.carouselInterval)
      }
    },
    nextSlide() {
      this.currentIndex = (this.currentIndex + 1) % this.items.length
    },
    prevSlide() {
      this.currentIndex = (this.currentIndex - 1 + this.items.length) % this.items.length
    },
    goToSlide(index) {
      this.currentIndex = index
    },
    handleCarouselClick(link) {
      if (link) {
        this.$router.push(link)
      }
    },
  },
}
</script>

<style scoped>
.carousel-container {
  position: relative;
  width: 100%;
  height: 0;
  padding-bottom: 45%; /* 设置合适的宽高比 */
  overflow: hidden;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  margin-bottom: 24px;
}

.carousel-wrapper {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

.carousel-item {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  transition: opacity 0.5s ease;
  cursor: pointer;
}

.carousel-item.active {
  opacity: 1;
  z-index: 1;
}

.carousel-bg {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  filter: blur(20px);
  transform: scale(1.1);
  z-index: 1;
}

.carousel-image {
  position: relative;
  z-index: 2;
  width: 100%;
  height: 100%;
  object-fit: contain;
  background-color: #000;
  transition: opacity 0.5s ease;
}

.carousel-caption {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 20px 30px;
  background: linear-gradient(transparent, rgba(0,0,0,0.8));
  color: white;
  z-index: 3;
}

.carousel-badge {
  display: inline-block;
  background: #FB7299;
  color: white;
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 12px;
  margin-right: 8px;
  font-weight: bold;
  vertical-align: middle;
}

.carousel-caption h3 {
  margin: 8px 0 10px 0;
  font-size: 24px;
  font-weight: 600;
}

.carousel-caption p {
  margin: 0;
  font-size: 14px;
  opacity: 0.9;
}

.carousel-indicators {
  position: absolute;
  bottom: 15px;
  right: 30px;
  z-index: 4;
  display: flex;
  gap: 8px;
}

.indicator {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.5);
  border: none;
  cursor: pointer;
  transition: all 0.3s ease;
}

.indicator.active {
  width: 24px;
  border-radius: 4px;
  background: white;
}

.carousel-arrow {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  width: 50px;
  height: 50px;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.3);
  color: white;
  border: none;
  font-size: 24px;
  cursor: pointer;
  transition: all 0.3s ease;
  z-index: 4;
  display: flex;
  align-items: center;
  justify-content: center;
}

.carousel-arrow:hover {
  background: rgba(0, 0, 0, 0.5);
  transform: translateY(-50%) scale(1.1);
}

.carousel-arrow.prev {
  left: 20px;
}

.carousel-arrow.next {
  right: 20px;
}

/* 响应式设计 */
@media (max-width: 992px) {
  .carousel-container {
    padding-bottom: 50%; /* 调整宽高比 */
  }
  
  .carousel-caption {
    padding: 20px 30px;
  }
  
  .carousel-caption h3 {
    font-size: 20px;
  }
  
  .carousel-indicators {
    right: 30px;
  }
}

@media (max-width: 768px) {
  .carousel-container {
    padding-bottom: 56.25%; /* 16:9 宽高比 */
  }
  
  .carousel-caption {
    padding: 15px 20px;
  }
  
  .carousel-caption h3 {
    font-size: 18px;
  }
  
  .carousel-caption p {
    font-size: 14px;
  }
  
  .carousel-arrow {
    width: 40px;
    height: 40px;
    font-size: 20px;
  }
}

@media (max-width: 576px) {
  .carousel-container {
    padding-bottom: 66.67%; /* 3:2 宽高比，更适合移动端 */
  }
  
  .carousel-caption {
    padding: 12px 16px;
  }
  
  .carousel-caption h3 {
    font-size: 16px;
  }
  
  .carousel-caption p {
    display: none;
  }
}
</style>